package ranger.clips
{
    import com.lowke.animator.Animator;
    import com.lowke.animator.Anime;
    
    import starling.display.MovieClip;
    import starling.textures.Texture;
    import starling.textures.TextureAtlas;
    
    public class Clip extends MovieClip
    {
        [Embed(source="/assets/Ranger.xml", mimeType="application/octet-stream")]
        private static const AnimationData:Class;
        
		/*
		[Embed(source="/assets/Ranger.atf", mimeType="application/octet-stream")]
		private static const AnimationTexture:Class;
		*/
		
		[Embed(source="/assets/Ranger.png")]
		private static var AnimationTexture:Class;
		
        private static var _textureAtlas:TextureAtlas;
        
        
        protected var _anime:Anime;
        
        public function Clip(stAssetName:String,
                             fRandomPosition:Boolean = false)
        {
            if (! _textureAtlas)
            {
                // create texture atlas
                var texture:Texture = Texture.fromBitmap(new AnimationTexture());
				// var texture:Texture = Texture.fromAtfData(new AnimationTexture() as ByteArray);
                var xml:XML = XML(new AnimationData());
                _textureAtlas = new TextureAtlas(texture, xml);
				
				/*
				var cubeTexture:CubeTexture = context3D.createCubeTexture(512, Context3DTextureFormat.COMPRESSED, false);
				var cubeTextureAsset:ByteArray = new CubeTextureAsset() as ByteArray;
				cubeTexture.uploadCompressedTextureFromByteArray(cubeTextureAsset, 0);
				*/
				
            }
            
            var textures:Vector.<Texture> = _textureAtlas.getTextures(stAssetName)
                
            super(textures, 60);
            
            // center the registration point (pivot)
            pivotX = width/2;
            pivotY = height/2;
            
            if (fRandomPosition)
            {
                // randomly position
                x = randomInt(width, RussellStarling.WIDTH - width);
                y = randomInt(height, RussellStarling.HEIGHT - height);
            }
            
            _anime = Animator.anime(this);
        }
        
        /**
         * Generates a random integer between low and high.
         * 
         * @param low Lowest possible integer generated.
         * @param high Highest possible integer generated.
         * @return Returns a random integer between low and high.
         */
        public static function randomInt(low:int, high:int):int 
        {
            var range:int = (high + 1) - low;
            var r:int = Math.floor(Math.random() * range) + low;
            
            return r;
        }
    }
}